﻿
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Bootstrap5.Abstractions;

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;

namespace Bootstrap5;

/// <summary>
/// Represents &lt;table> element.
/// </summary>
[CssClass("table")]
[HtmlTag("table")]
public class Table : TableComponentBase
{
    /// <summary>
    /// Sets the content of &lt;thead> element.
    /// </summary>
    [Parameter] public RenderFragment HeadContent { get; set; }
    /// <summary>
    /// Sets the content of &lt;tfoot> element.
    /// </summary>
    [Parameter] public RenderFragment FootContent { get; set; }

    /// <summary>
    /// Sets the content of &lt;tbody> element.
    /// </summary>
    [Parameter]public override RenderFragment ChildContent { get; set; }
    [Parameter] public RenderFragment CaptionContent { get; set; }
    /// <summary>
    /// To add zebra-striping to any table row.
    /// </summary>
    [Parameter] [CssClass("table-striped")] public bool Striped { get; set; }
    /// <summary>
    /// To enable a hover state on table rows.
    /// </summary>
    [Parameter] [CssClass("table-hover")] public bool Hover { get; set; }
    /// <summary>
    /// For borders on all sides of the table and cells.
    /// </summary>
    [Parameter] [CssClass("table-bordered")] public bool Bordered { get; set; }
    /// <summary>
    /// For a table without borders.
    /// </summary>
    [Parameter] [CssClass("table-borderless")] public bool Borderless { get; set; }
    /// <summary>
    /// To make any table more compact by cutting all cell padding in half.
    /// </summary>
    [Parameter] [CssClass("table-sm")] public bool Small { get; set; }
    /// <summary>
    /// Put the caption on the top of the table
    /// </summary>
    [Parameter] [CssClass("caption-top")] public bool CaptionTop { get; set; }
    /// <summary>
    /// Scroll table horizontally to across every breakpoint.
    /// </summary>
    [Parameter] public bool Responsive { get; set; }
    [Parameter] public BreakPoint[] ResponsiveBreakPoints { get; set; }

    protected override void BuildRenderTree(RenderTreeBuilder builder)
    {
        if (Responsive)
        {
            var classList = new List<string> { "table-responsive" };
            if(ResponsiveBreakPoints is not null)
            {
                classList.AddRange(ResponsiveBreakPoints.Select(m => $"table-responsive-{m.GetEnumCssClass()}"));
            }

            builder.OpenElement(0, "div");
            builder.AddAttribute(1, "class", string.Join(" ", classList));
            builder.AddContent(2, BuildTable);
            builder.CloseElement();
        }
        else
        {
            BuildTable(builder);
        }
    }

    private void BuildTable(RenderTreeBuilder builder)
    {
        builder.OpenElement(0, GetHtmlTagName());
        BuildCommonAttributes(builder);
        this.CreateCascadingValueComponent<Table>(builder, BuildTableContent);
        builder.CloseElement();
    }

    private void BuildTableContent(RenderTreeBuilder builder)
    {
        if(CaptionContent is not null)
        {
            builder.AddContent(20, content => BuildContent(content, "caption", CaptionContent));
        }
        if (HeadContent is not null)
        {
            builder.AddContent(30,content=> BuildContent(content,"thead", HeadContent));
        }
        builder.AddContent(40, content => BuildContent(content, "tbody", ChildContent));

        if (FootContent is not null)
        {
            builder.AddContent(50, content => BuildContent(content, "tfoot", FootContent));
        }
    }

    private void BuildContent(RenderTreeBuilder builder,string elementName,RenderFragment content)
    {
        builder.OpenElement(0, elementName);
        builder.AddContent(1, content);
        builder.CloseElement();
    }
}



/// <summary>
/// Represents a new header cell in <see cref="TableRow"/> component as &lt;th> element.
/// </summary>
[HtmlTag("th")]
public class TableHead: TableColumn
{
}

/// <summary>
/// Represents a new cell in <see cref="TableRow"/> component as &lt;td> element.
/// </summary>
[HtmlTag("td")]
public class TableCell : TableColumn
{
}

public abstract class TableColumn: TableComponentBase
{
    protected internal TableColumn() { }

    [Parameter][HtmlProperty("colspan")]public int? ColSpan { get; set; }
    [Parameter][HtmlProperty("rowspan")] public int? RowSpan { get; set; }
    /// <summary>
    /// Highlight a table cell.
    /// </summary>
    [Parameter] [CssClass("table-active")] public bool Active { get; set; }
}

/// <summary>
/// Represents a new row in <see cref="Table"/> component as &lt;tr> element.
/// </summary>
[HtmlTag("tr")]
public class TableRow : TableComponentBase
{
    /// <summary>
    /// Highlight a table row.
    /// </summary>
    [Parameter] [CssClass("table-active")] public bool Active { get; set; }
}

/// <summary>
/// The table base component in Bootstrap.
/// </summary>
public abstract class TableComponentBase:BootstrapChildContentComponentBase
{
    /// <summary>
    /// Initializes an instance of <see cref="TableComponentBase"/> class.
    /// </summary>
    protected internal TableComponentBase() { }
    /// <summary>
    /// The color of component.
    /// </summary>
    [Parameter] [CssClass("table-")] public virtual Color? Color { get; set; }
}